
import UIKit

private let reuseIdentifier = "Cell"
private var count: Int = 4///< 总个数

class XSNewGuideController: UICollectionViewController, XSNewGuideCellDelegate {
    
    // MARK: - 属性
    let flowLayout = UICollectionViewFlowLayout()///< 流水布局
    
    init()
    {
        super.init(collectionViewLayout: flowLayout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(XSNewGuideCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        self.flowLayout.itemSize = UIScreen.mainScreen().bounds.size
        self.flowLayout.minimumInteritemSpacing = 0
        self.flowLayout.minimumLineSpacing = 0
        self.collectionView?.pagingEnabled = true
        self.flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    // MARK: - UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! XSNewGuideCell
        
        cell.index = indexPath.item
        cell.delegate = self
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
    {
        // 根据当前显示的cell来获得当前的item
        let indexPath = collectionView.indexPathsForVisibleItems().last!
        
        if indexPath.item == count - 1
        {
            // 获得当前item的cell
            let cell = collectionView.cellForItemAtIndexPath(indexPath) as! XSNewGuideCell
            
            cell.startAnimate()
        }
    }
    
    func startBtnDidClick()
    {
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchVc(XSTabBarViewController() as XSTabBarViewController)
    }
}